package com.ujoku.controller;

import com.labillusion.core.platform.exception.ResourceNotFoundException;
import com.labillusion.core.platform.web.rest.RESTController;
import com.ujoku.domain.Client;
import com.ujoku.domain.Country;
import com.ujoku.domain.GCategory;
import com.ujoku.domain.Winery;
import com.ujoku.domain.admin.Parameter;
import com.ujoku.service.*;
import com.ujoku.service.admin.ParametersService;
import com.ujoku.view.domain.TopNavigationView;
import com.ujoku.view.domain.TopWineryView;
import com.ujoku.view.domain.WineryView;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;
import java.util.stream.Collectors;

@Controller
public class IndexController extends RESTController{

    @Autowired
    private ClientService clientService;

    @Autowired
    private GCategoryService categoryService;

    @Autowired
    private CountryService countryService;

    @Autowired
    private ParametersService parametersService;

    @Autowired
    private WineryService wineryService;

    @Autowired
    private CdnService cdnService;


    @RequestMapping(value="/", method = RequestMethod.GET)
    public String Index(ModelMap model) throws Exception {

        List<Client> list = clientService.selectList(null);
        model.put("clientList", list);

        return "index";
    }

    @RequestMapping(value="/test-tool", method = RequestMethod.GET)
    public String testTool(ModelMap model) throws Exception {

        Map<String,Object> query = new HashMap<>();
        query.put("ClientName","OA");

        List<Client> list = clientService.selectList(query);
        model.put("clientList", list);

        return "tool";
    }


    @RequestMapping(value="/topNavigation", method = RequestMethod.GET)
    @ResponseBody
    public TopNavigationView getTopNavigationView(){
        TopNavigationView view = new TopNavigationView();
        List<GCategory> categoryList = categoryService.selectList();

        view.setCategoryList(categoryList.stream().filter(c->c.getParent_id() == 0).collect(Collectors.toList()));

        List<Country> countryList = countryService.selectList();
        view.setCountryList(countryList);

        Parameter pGrade = parametersService.getParameterByKey("grade");
        if(pGrade != null)
            view.setGradeList(Arrays.asList(pGrade.getValue().split(",")));

        Parameter pIngredient = parametersService.getParameterByKey("ingredient");
        if(pIngredient != null)
            view.setIngredientList(Arrays.asList(pIngredient.getValue().split(",")));

        List<TopWineryView> wineryViewList = new ArrayList<>();
        List<Winery> wineryList = wineryService.selectList();
        wineryList.forEach(w->{
            TopWineryView wineryView = new TopWineryView();
            BeanUtils.copyProperties(w, wineryView);
            wineryView.setImage(w.getImageList().get(1));
            wineryViewList.add(wineryView);
        });

        view.setWineryList(wineryViewList);

        return view;
    }

    @RequestMapping(value = "/winery/{id}", method = RequestMethod.GET)
    @ResponseBody
    public WineryView getWineryById(@PathVariable int id){

        List<Winery> wineryList = wineryService.selectList();

        WineryView view = new WineryView();
        Winery winery = wineryList.stream().filter(w->w.getId() == id).findFirst().orElse(null);
        if(winery == null)
            throw new ResourceNotFoundException("can not found winery");

        view.setWinery(winery);

        List<TopWineryView> wineryViewList = new ArrayList<>();
        wineryList.forEach(w->{
            TopWineryView wineryView = new TopWineryView();
            BeanUtils.copyProperties(w, wineryView);
            wineryViewList.add(wineryView);
        });

        view.setWineryViewList(wineryViewList);

        return view;
    }
}